<template>
  <div class="login-form">
    <h2 class="form-title">登录</h2>
    <el-form ref="loginFormRef" :model="loginForm" :rules="rules" label-position="top">
      <el-form-item label="用户名" prop="username">
        <el-input 
          v-model="loginForm.username" 
          placeholder="请输入用户名"
          prefix-icon="User"
        ></el-input>
      </el-form-item>
      
      <el-form-item label="密码" prop="password">
        <el-input 
          v-model="loginForm.password" 
          type="password" 
          placeholder="请输入密码" 
          show-password
          prefix-icon="Lock"
        ></el-input>
      </el-form-item>
      
      <div class="form-options">
        <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        <a href="#" class="forgot-password">忘记密码?</a>
      </div>
      
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="submitForm" style="width: 100%">
          <span class="login-text">登录</span>
          <el-icon class="login-icon"><ArrowRight /></el-icon>
        </el-button>
      </el-form-item>
      
      <div class="tip-wrapper">
        <div class="tip">
          <el-alert
            title="测试账号"
            type="info"
            :closable="false"
            description="普通账号: test/test123, Pro账号: admin/admin123"
          ></el-alert>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useStore } from 'vuex';
import { ElMessage } from 'element-plus';
import { User, Lock, ArrowRight } from '@element-plus/icons-vue';
import { AuthService } from '../../services';

const emit = defineEmits(['login-success', 'switch-mode']);
const store = useStore();
const loading = ref(false);
const rememberMe = ref(false);

// 定义表单数据
const loginForm = reactive({
  username: '',
  password: ''
});

// 验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3到20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
  ]
};

// 登录表单引用
const loginFormRef = ref(null);

// 提交表单
const submitForm = async () => {
  if (!loginFormRef.value) return;
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      
      try {
        // 调用登录API
        const response = await AuthService.login({
          username: loginForm.username,
          password: loginForm.password
        });
        
        // 检查并安全获取用户信息
        const responseData = response.data || {};
        const userInfo = responseData.user || {
          username: loginForm.username,
          id: responseData.userId || 0,
          // 根据后端返回数据结构设置默认值
          email: responseData.email || '',
          role: responseData.role || 'user',
          isPro: responseData.isPro || false
        };
        
        // 更新Vuex store中的用户信息
        store.commit('setUser', userInfo);
        
        ElMessage.success('登录成功');
        emit('login-success', userInfo);
      } catch (error) {
        ElMessage.error(error.message || '登录失败，请检查用户名和密码');
      } finally {
        loading.value = false;
      }
    }
  });
};
</script>

<style scoped>
.login-form {
  max-width: 500px;
  margin: 0 auto;
  padding: 40px;
  border-radius: 20px;
  background-color: #fff;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  transition: all 0.4s ease;
  transform: translateZ(0);
  overflow: hidden;
}

.form-title {
  font-size: 32px;
  font-weight: 700;
  color: var(--black);
  margin-bottom: 35px;
  text-align: center;
  background: linear-gradient(120deg, var(--primary-color), var(--primary-light));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 0.5px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.forgot-password {
  color: var(--primary-color);
  text-decoration: none;
  font-size: 15px;
  transition: all 0.3s ease;
  position: relative;
  padding-bottom: 2px;
}

.forgot-password:after {
  content: '';
  position: absolute;
  width: 0;
  height: 2px;
  bottom: 0;
  left: 0;
  background-color: var(--primary-color);
  transition: width 0.3s ease;
}

.forgot-password:hover {
  color: var(--primary-dark);
}

.forgot-password:hover:after {
  width: 100%;
}

:deep(.el-input__wrapper) {
  border-radius: 12px;
  height: 50px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04), 0 0 0 1px rgba(0, 0, 0, 0.04);
  padding: 0 16px;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.09), 0 0 0 1px rgba(0, 0, 0, 0.09);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 3px 15px rgba(var(--primary-color-rgb), 0.15), 0 0 0 2px rgba(var(--primary-color-rgb), 0.1);
}

:deep(.el-input__inner) {
  font-size: 16px;
  height: 46px;
  padding: 0 12px;
}

:deep(.el-input__prefix) {
  padding-right: 12px;
}

:deep(.el-input__suffix) {
  padding-left: 12px;
}

:deep(.el-input__icon) {
  font-size: 18px;
  line-height: 46px;
  color: var(--primary-color);
}

/* 处理错误状态边框显示问题 */
:deep(.el-form-item.is-error .el-input__wrapper) {
  box-shadow: 0 0 0 1px var(--el-color-danger) !important;
  border-radius: 12px;
  overflow: visible;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}

:deep(.el-button) {
  border-radius: 12px;
  height: 52px;
  font-weight: 600;
  font-size: 16px;
  letter-spacing: 0.5px;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  margin-top: 15px;
}

:deep(.el-button--primary) {
  background: linear-gradient(120deg, var(--primary-color) 0%, var(--primary-light) 100%);
  border: none;
  box-shadow: 0 6px 15px rgba(var(--primary-color-rgb), 0.3);
}

:deep(.el-button--primary:hover) {
  box-shadow: 0 8px 25px rgba(var(--primary-color-rgb), 0.5);
  transform: translateY(-2px);
}

:deep(.el-button--primary:active) {
  transform: translateY(0);
  box-shadow: 0 4px 10px rgba(var(--primary-color-rgb), 0.3);
}

:deep(.el-form-item__label) {
  font-weight: 600;
  font-size: 15px;
  color: var(--black);
  padding-bottom: 8px;
}

.tip-wrapper {
  margin-top: 32px;
}

:deep(.el-alert) {
  border-radius: 12px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  padding: 15px;
}

:deep(.el-alert:hover) {
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

:deep(.el-alert__title) {
  font-weight: 600;
  font-size: 15px;
}

:deep(.el-alert__description) {
  font-size: 14px;
  line-height: 1.5;
  margin: 5px 0 0;
}

.login-text {
  transition: transform 0.3s ease;
  display: inline-block;
  margin-right: 8px;
}

.login-icon {
  transition: transform 0.3s ease;
  margin-left: 8px;
  font-size: 18px;
}

:deep(.el-button--primary:hover .login-text) {
  transform: translateX(-5px);
}

:deep(.el-button--primary:hover .login-icon) {
  transform: translateX(5px);
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
}

:deep(.el-checkbox__label) {
  font-size: 15px;
}

@media (max-width: 768px) {
  .login-form {
    padding: 30px;
  }
  
  .form-title {
    font-size: 28px;
    margin-bottom: 30px;
  }
}

@media (max-width: 480px) {
  .login-form {
    padding: 25px 20px;
    border-radius: 16px;
  }
  
  .form-title {
    font-size: 26px;
    margin-bottom: 25px;
  }
  
  :deep(.el-input__wrapper),
  :deep(.el-input__inner) {
    height: 48px;
  }
  
  :deep(.el-button) {
    height: 48px;
  }
}
</style> 